package net.tycmc.cty.ctyms.timejob.iems.dao;

import java.util.List;
import java.util.Map;

import net.tycmc.bulb.common.dao.IBaseDAO;
import net.tycmc.bulb.common.dao.ITransactionDAO;
import net.tycmc.bulb.common.util.MapGetter;
import net.tycmc.bulb.common.util.StringUtil;

public class FileToFTPDAO {
	private IBaseDAO baseDAO;
	private ITransactionDAO tranDAO;
	
	public IBaseDAO getBaseDAO() {
		return baseDAO;
	}
	public void setBaseDAO(IBaseDAO baseDAO) {
		this.baseDAO = baseDAO;
	}
	public ITransactionDAO getTranDAO() {
		return tranDAO;
	}
	public void setTranDAO(ITransactionDAO tranDAO) {
		this.tranDAO = tranDAO;
	}
	
	//获取CSV文件夹路径
	public String getCsvPath(){
		String sql = "select top 1 Sys_Url_Repath from Sys_FileURL where Sys_Url_Type = 2";
		Map<String,Object> map = baseDAO.execQueryOne(sql, new Object[]{});
		return MapGetter.getString(map, "Sys_Url_Repath");
	}
	
	//获取标记表延迟数据类型最大ID值
	public String getMaxId(){
		String sql = "select top 1 StatR_StatDataID from StatRecord where StatR_Type=4 order by StatR_ID desc";
		Map<String,Object> map = baseDAO.execQueryOne(sql, new Object[]{});
		return MapGetter.getString(map, "StatR_StatDataID");
	}
	
	//更新标记表延迟数据类型最大ID值
	public boolean upMaxID(String upMaxID){
		String sql = "update StatRecord set StatR_StatDataID=?,StatR_UpdateTime=GETDATE() where StatR_Type=4";
		int c = baseDAO.execUpdate(sql, new Object[]{upMaxID});
		if(c>0){
			return true;
		}else{
			return false;
		}
	}
	
	//获取延迟数据时间集合
	public List<Map<String,Object>> getTimes(String maxID){
		String sql = "select convert(varchar(10),MsgBF_Time,120) as Times,MAX(msgbf_id) as msgbf_id from CTY_Egn_B.dbo.Msg_BufferRecord " 
				+"where msgbf_id>? and MsgBF_MsgTp_ID in (4,5,6) group by convert(varchar(10),MsgBF_Time,120) "
				+"order by msgbf_id ";
		return baseDAO.execQuery(sql, new Object[]{maxID});
	}
	
	//获取延迟设备集合
	public List<Map<String,Object>> getVclsOfSoft(String softType,String maxID,String time,String MsgBF_MsgTp_IDs){
		String sql = "select VehicleNumber,VehicleID from ( "
			+"select MsgBF_Vcl_ID from CTY_Egn_B.dbo.Msg_BufferRecord "
			+"where MsgBF_ID>? and convert(varchar(10),MsgBF_Time,120)=? "
			+"and MsgBF_MsgTp_ID in ("+MsgBF_MsgTp_IDs+") group by MsgBF_Vcl_ID)tmp "
			+"left join V_VehicleInfo on VehicleID=MsgBF_Vcl_ID "
			+"where UsefulLife>=GETDATE() and Tmnl_SoftEdition<>'' "
			+"and Tmnl_SoftEdition is not null ";
		if(StringUtil.isValid(softType)){
			sql += "and Tmnl_SoftEdition in ("+softType+")";
		}
		return baseDAO.execQuery(sql, new Object[]{maxID,time});
	}
}